package com.aluka.nirvana.framework.security.handler;

import com.aluka.nirvana.framework.security.utils.ResponseUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义 Security 用户未登陆处理类
 * @author gongli
 * @since 2019/4/10 13:40
 */
@Component
public class CustomAuthenticationEntryPointHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {
        ResponseUtils.writeResponse(httpServletResponse, HttpStatus.FORBIDDEN,"用户未登录或登录信息已过期!");
    }
}
